<?php
class TrainningController extends Zend_Controller_Action
{	
    public function init() {
        $SS = new Zend_Session_Namespace ('Default');
        Zend_Session::namespaceUnset('Default');
    }
    public function indexAction()
    {
        $this->view->title = "Khóa học";
        $modelDev = new Vina_Model_DeveloperMapper();
        $dataCat = $modelDev->TreeMenu(41);
        $listIdCat[] = 41;
        foreach($dataCat as $vCat):
            $listIdCat[] = $vCat['id'];
        endforeach;
        
        $stringlistId = implode(',', $listIdCat);
        
        $news                    = new Vina_Model_News();
        $dataNews                = $news->fetchListToArray("category_id IN($stringlistId)", 'id desc');
        $paginator         = Zend_Paginator::factory($dataNews);
            $currentPage       = 1;
            //Check if the user is not on page 1
            $page              = $this->_getParam('page');
            if (! empty($page)) { //Where page is the current page
                $currentPage   = $this->_getParam('page');
            }
            //Set the properties for the pagination
            $paginator->setItemCountPerPage(6);
            $paginator->setPageRange(6);
            $paginator->setCurrentPageNumber($currentPage);
            Zend_Paginator::setDefaultScrollingStyle('Sliding');
            Zend_View_Helper_PaginationControl::setDefaultViewPartial('pagination.phtml');
            $paginator->setView($this->view);
           
            $this->view->data = $paginator;
    }
    public function filterAction(){
        $id                     =  $this->_request->getParam('id',0);
        $modelCat = new Vina_Model_Categories();
        $dataCategory = $modelCat->find($id)->toArray();
        
        $modelDev = new Vina_Model_DeveloperMapper();
        $dataCat = $modelDev->TreeMenu($id);
        $listIdCat[] = 41;
        foreach($dataCat as $vCat):
            $listIdCat[] = $vCat['id'];
        endforeach;
        
        $stringlistId = implode(',', $listIdCat);
        
        $news                    = new Vina_Model_News();
        $dataNews                = $news->fetchListToArray("category_id IN($id)", 'id desc');
        $paginator         = Zend_Paginator::factory($dataNews);
            $currentPage       = 1;
            //Check if the user is not on page 1
            $page              = $this->_getParam('page');
            if (! empty($page)) { //Where page is the current page
                $currentPage   = $this->_getParam('page');
            }
            //Set the properties for the pagination
            $paginator->setItemCountPerPage(6);
            $paginator->setPageRange(6);
            $paginator->setCurrentPageNumber($currentPage);
            Zend_Paginator::setDefaultScrollingStyle('Sliding');
            Zend_View_Helper_PaginationControl::setDefaultViewPartial('pagination.phtml');
            $paginator->setView($this->view);
            $this->view->title = $dataCategory['name'];
            $this->view->dataHead = $dataCategory['name'];
            $this->view->data = $paginator;
            $this->render('index');
    }
    public function detailAction() {
        $news                    = new Vina_Model_News();
        $id                     =  $this->_request->getParam('id',0);
        $data = $news->find($id)->toArray();
        $category_id = $data['category_id'];
        $this->view->title = $data['title'];
        $otherData = $news->fetchListToArray("id != $id and category_id = $category_id",'id desc', 10);
        $this->view->data = $data;
        $this->view->otherData = $otherData;
    }
    public function searchprocessAction(){
        $formData = $this->_request->getPost();
        $keyword =  $formData['keyword'];
        if($keyword == NULL){
            $this->_redirect($_SERVER['HTTP_REFERER']);
        }else{
            $this->_redirect("/tim-kiem/".$keyword);
        }
    }
    public function searchAction(){
        $keyword                     =  $this->_request->getParam('keyword',0);
    
        $this->view->title = "Tìm kiếm khóa học";
        $modelDev = new Vina_Model_DeveloperMapper();
        $dataCat = $modelDev->TreeMenu(41);
        $listIdCat[] = 41;
        foreach($dataCat as $vCat):
            $listIdCat[] = $vCat['id'];
        endforeach;
        
        $stringlistId = implode(',', $listIdCat);
        
        $news                    = new Vina_Model_News();
        $dataNews                = $news->fetchListToArray("category_id IN($stringlistId and title like '%".$keyword."%')", 'id desc');
        $paginator         = Zend_Paginator::factory($dataNews);
            $currentPage       = 1;
            //Check if the user is not on page 1
            $page              = $this->_getParam('page');
            if (! empty($page)) { //Where page is the current page
                $currentPage   = $this->_getParam('page');
            }
            //Set the properties for the pagination
            $paginator->setItemCountPerPage(6);
            $paginator->setPageRange(6);
            $paginator->setCurrentPageNumber($currentPage);
            Zend_Paginator::setDefaultScrollingStyle('Sliding');
            Zend_View_Helper_PaginationControl::setDefaultViewPartial('pagination.phtml');
            $paginator->setView($this->view);
           
            $this->view->data = $paginator;
            $this->render('index');
    }
    	
}
